Skip to content

Fix wrong exception propagation on connection errors and channel binding failure#1676

Open
tsegismont wants to merge 1 commit into
eclipse-vertx:masterfrom
tsegismont:connection-errors
Open

Fix wrong exception propagation on connection errors and channel binding failure#1676
tsegismont wants to merge 1 commit into
eclipse-vertx:masterfrom
tsegismont:connection-errors

Conversation

@tsegismont

Copy link
Copy Markdown
Member

See #1672, #1674, #1675

When a connection handles a failure, the underlying Netty socket should be closed, avoiding leak of file descriptors and Netty resources.

Additionally, inflight commands shouldn't fail with ClosedConnectionException but with the actual error.

Some portions of this content were created with the assistance of Claude Code.

@tsegismont tsegismont requested a review from vietj June 16, 2026 16:30
@tsegismont

tsegismont commented Jun 16, 2026

Copy link
Copy Markdown
Member Author

@vietj can you please take a look? This is the fix for problems introduced by c684c42

@jorsol

jorsol commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Hi @tsegismont

I tested your branch but I don't get the real message, it seems that it still returns the ClosedConnectionException instead of the actual error.

Could you please confirm what I'm doing wrong?

    Vertx vertx = Vertx.vertx();
    PgConnectOptions connectOptions = new PgConnectOptions()
        .setPort(5432)
        .setHost("localhost")
        .setDatabase("postgres")
        .setUser("postgres")
        .setSslOptions(new ClientSSLOptions().setTrustAll(true))
        .setSslMode(SslMode.DISABLE)
        .setChannelBinding(io.vertx.pgclient.ChannelBinding.REQUIRE)
        .setPassword("123");

    PgConnection.connect(vertx, connectOptions)
        .onComplete(res -> {
          if (res.succeeded()) {
            System.out.println("Connected...");
          } else {
            System.out.println("Could not connect: " + res.cause().getMessage());
          }
        });
    vertx.close();

@vietj

vietj commented Jun 17, 2026

Copy link
Copy Markdown
Member

my branch does not aim to fix the bug we are currently observing in this issue, it aims first to get the exception handling flow correct from the NetSocket / SocketConnectionBase perspective before doing any other fix.

@vietj

vietj commented Jun 17, 2026

Copy link
Copy Markdown
Member

in particular : handleException should not close the socket in your patch because the socket will be closed by the code calling handleException, this is what I'd like to clarify and avoid.

@tsegismont

Copy link
Copy Markdown
Member Author

@vietj please take another look

I had to make a special case in InitPgCommandMessage.java, because if we let the exception bubble up, then:

  • the client tries to close the channel gracefully
  • it sends a close command
  • Postgres replies with a failure like "invalid state"
  • the user gets the "invalid state failure" instead of the sasl failure

Nevertheless, I don't believe we have a way to close the connection abruptly like PostgreSQL expects:

If the frontend does not support the authentication method requested by the server, then it should immediately close the connection.

https://www.postgresql.org/docs/current/protocol-flow.html

I believe this would require changes in VertxConnection. Thoughts?

@tsegismont tsegismont changed the title Fix socket leak and wrong exception propagation on connection error Fix wrong exception propagation on connection errors and channel binding failure Jun 19, 2026
…ing failure

See eclipse-vertx#1672, eclipse-vertx#1674, eclipse-vertx#1675

Inflight commands should not fail with ClosedConnectionException but with the
actual error, e.g. a PgException reporting SQLSTATE 57P01 when
pg_terminate_backend kills a connection mid-flight.

When channel binding is required but SSL is not available, the PostgreSQL
protocol recommends immediately closing the connection. The
ChannelBindingException is now correctly reported to the caller rather than
being masked by a subsequent ClosedConnectionException.

Some portions of this content were created with the assistance of Claude Code.

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>

Fixup

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
@tsegismont

Copy link
Copy Markdown
Member Author

Well in fact, encoder.close() does exactly that 😓

@vietj

vietj commented Jun 19, 2026

Copy link
Copy Markdown
Member

your changes in InitPgCommandMessage makes sense to me and it looks more idiomatic to proceed this way.

@vietj

vietj commented Jun 19, 2026

Copy link
Copy Markdown
Member

another way would be to close the ChannelHandlerContext ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants